<?php

namespace IDock\Insurance\AMIC\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\AMIC\AMICEnums;
use IDock\Insurance\AMIC\Request\VehicleModelRequest;
use IDock\Printer\VehicleModelPrinter;
use IDock\Printer\VehiclePrinter;

class VehicleModelResponse extends AllAMICResponse
{

    /**
     * visitor
     * @var  VehicleModelRequest
     */
    public $visitor;

    /**
     * @var VehicleModelPrinter $printer
     */
    public $printer = VehicleModelPrinter::class;

    public function printerInit(): Response
    {
        $body = $this->getBody()['Body'] ?? [];
        $modelList = $body['vehicleQueryList'] ?? [];
        if (!$modelList) {
            return $this;
        }
        $vehicleModelCode = $this->visitor->argc['vehicleModelCode'] ?? '';

        foreach (($modelList['vehicleQuery'] ?? []) as $datum) {

            $modelName = $datum['modelName'] ?? '';

            if (!is_int(strpos($modelName, $vehicleModelCode))) {
                continue;
            }

            $vehiclePrinter = new VehiclePrinter();
            $vehiclePrinter->vehicleName = $modelName;
            $vehiclePrinter->vehicleModelCode = $datum['vehicleId'] ?? '';
            $vehiclePrinter->brandName = $datum['vehicleBrand'] ?? '';
            $vehiclePrinter->vehicleExhaust = $datum['exHaustScale'] ?? '';
            $vehiclePrinter->power = $datum['vehiclePower'] ?? '';
            //进口车标识
//            $vehicleType = $datum['countryCode'] == '国产车' ? 'I1' : 'I2';
//            $vehiclePrinter->vehicleImport = $vehicleType;
//            $vehiclePrinter->vehicleImportName = IDockEnums::$importFlag[$vehicleType];
            $vehiclePrinter->vehiclePrice = $datum['purchasePrice'] ?? '';
            $vehiclePrinter->vehiclePriceWithOutTax = $datum['purchasePrice'] ?? '';
            $vehiclePrinter->vehicleSeat = $datum['seatCount'] ?? '';
            $vehiclePrinter->familyName = $datum['familyName'] ?? '';
            $vehiclePrinter->remark = $datum['remark'] ?? '';
            $vehiclePrinter->vehicleYear = $datum['caryear'] ? date('Y-m-d', strtotime($datum['caryear'])) : '';
            $tonCount = $datum['tonCount'] ?? 0;
            $vehiclePrinter->vehicleTonnage = $tonCount * 1000;
            $vehiclePrinter->wholeWeight = $datum['emptyWeight'] ? round($datum['emptyWeight'] * 1000, 2) : '0';

            //能源类型
            $powerTypeCode = $datum['fuelType'] ?? '0';
            $vehiclePrinter->energyTypeCode = array_search($powerTypeCode, AMICEnums::$energyType) ?: 'E00';
            $vehiclePrinter->energyTypeName = IDockEnums::$energyType[$vehiclePrinter->energyTypeCode];
            //新能源
            if (in_array($vehiclePrinter->energyTypeCode, ['E02', 'E03', 'E04'])) {
                $vehiclePrinter->isNewEnergy = '1';
            }
            $this->printer->setModelList($vehiclePrinter);
        }

        return $this;
    }
}
